package com.tianqi.tools;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.jfinal.log.Log;



public class ObjectTools {
    static Log logger = Log.getLog(ObjectTools.class);

	/**
	 * 基本对象操作工具
	 * 
	 */
	// 转换为中文
	public static String toChi1(String input) {
		try {
			if (input == null) {
				input = "";
			}
//			byte[] bytes = input.getBytes("ISO8859-1");
//			return new String(bytes);
			return new String(input.getBytes("ISO-8859-1"), "gbk");
		} catch (Exception ex) {
		}
		return null;
	}

	// 字符串转Int
	public static int toInt(Object value, int defaultInt) {
		try {
			String str_value = toString(value);
			if (isNull(str_value)) {
				return defaultInt;
			} else {
				return Integer.parseInt(str_value);
			}
		} catch (Exception e) {
			return defaultInt;
		}
	}

	// 格式化为长整形
	public static long toLong(Object value, long defaultLong) {
		try {
			String str_value = toString(value);
			if (isNull(str_value)) {
				return defaultLong;
			} else {
				return Long.parseLong(str_value);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return defaultLong;
		}
	}

	// 转换字符串
	public static String toString(Object value) {
		String newString = "";
		try {
			if(value!=null){
				newString = String.valueOf(value).toString();
				if (newString == null || "".equals(newString)) {
					newString = "";
				}
			}else{
				newString = "";
			}
		} catch (Exception e) {
			//log.error("\n\n转换字符串出错！！－－－－－－－－>" + value);
			newString = "";
		}
		return newString;
	}

	// 判断字符串是否为空
	public static boolean isNull(String value) {
		boolean lb_return = true;
		if (value == null || value.length() == 0 || value.trim().length() == 0
				|| value.equals("null")) {
			lb_return = true;
		} else {
			lb_return = false;
		}
		return lb_return;
	}

	// 倒转list
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List invertList(List list) {
		List tem_list = new ArrayList();
		for (int i = list.size() - 1; i >= 0; i--) {
			tem_list.add(list.get(i));
		}
		list = null;
		return tem_list;
	}

	// 格式化数组为字符串
	public static String intToString(int num, String format) {
		DecimalFormat df = new DecimalFormat(format);
		String str_tem = "";
		str_tem = df.format(num);
		return str_tem;
	}

	// 格式化数组为字符串
	public static String longToString(long num, String format) {
		DecimalFormat df = new DecimalFormat(format);
		String str_tem = "";
		str_tem = df.format(num);
		return str_tem;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Set getRandomSet(int max, int num) {
		Set n_set = new HashSet();
		Random rnd = new Random();

		for (int i = 0; i < max && i < num; i++) {
			n_set.add(rnd.nextInt(max));
		}
		return n_set;
	}

	public static int getRandomInt(int max) {

		Random rnd = new Random();

		return rnd.nextInt(max);
	}

	/**
	 * 删除input字符串中的html格式
	 * 
	 * @param input
	 * @param length
	 * @return
	 */

	public static String filterHtmlAtHmtl(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		new_str = new_str.replaceAll("[(/>)<]", "");
		return new_str;

	}

	public static String filterHtmlAtJavaScript(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll("<script[\\s\\S]+</script *>", "");
		return new_str;

	}

	public static String filterHtmlAtOnEvent(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll(" on[\\s\\S]*=", " _disibledevent=");
		return new_str;

	}

	public static String filterHtmlAtHrefJavascript(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll(" href *= *[\\s\\S]*script *:", "");
		return new_str;

	}

	public static String filterHtmlAtIframe(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll("<iframe[\\s\\S]+</iframe *>", "");
		return new_str;

	}

	public static String filterHtmlAtFrameset(String str) {
		if (isNull(str)) {
			return "";
		}
		// 去掉所有html元素,
		String new_str = str.replaceAll("<frameset[\\s\\S]+</frameset *>", "");
		return new_str;

	}

	public static String filterHtmlAll(String str) {
		String new_str = filterHtmlAtJavaScript(str);
		new_str = filterHtmlAtHmtl(new_str);
		new_str = filterHtmlAtOnEvent(new_str);
		new_str = filterHtmlAtHrefJavascript(new_str);
		new_str = filterHtmlAtIframe(new_str);
		new_str = filterHtmlAtFrameset(new_str);
		return new_str;
	}
	public static Integer parseInt(String value,Integer returnValue){
		Integer result = returnValue;
		try {
			if (value == null || value.length() == 0|| value.trim().length() == 0 || value.equals("null")||"\n".equals(value)) {
				result = returnValue;
			} else {
				result = Integer.parseInt(value.trim());
			}
		} catch (Exception e) {
			logger.info("异常"+e);
		}
		return result;
	}
	public static Double parseDouble(String value,Double returnValue){
		Double result = returnValue;
		try {
			if (value == null || value.length() == 0
					|| value.trim().length() == 0 || value.equals("null")) {
				result = returnValue;
			} else {
				result = Double.parseDouble(value);
			}
		} catch (Exception e) {
			logger.info("异常"+e);
		}
		return result;
	}
	public static Date parseSqlDate(String value,String format){
		SimpleDateFormat sim=new SimpleDateFormat(format);
		Date result = null;
		try {
			if (value == null || value.length() == 0
					|| value.trim().length() == 0 || value.equals("null")) {
				result = null;
			} else {
				result = new java.sql.Date(sim.parse(value).getTime());
			}
		} catch (Exception e) {
			logger.info("异常"+e);
		}
		return result;
	}
	public static void main(String[] args) {
		System.out.println(parseInt("06",0));
	}
}
